home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / cli / Strings.lha / strings.c < prev    next >
C/C++ Source or Header  |  1994-12-03  |  3KB  |  140 lines

  1. ;/* Execute me to compile
  2.   SC IGNORE=73 NOSTACKCHECK SMALLCODE SMALLDATA STARTUP=cres NODEBUG OPTIMIZE LINK Strings.c
  3.   Quit
  4.   * Program: strings.c
  5.   * Author: Osma Ahvenlampi
  6.   * Description: search for ASCII strings in files
  7.   */
  8.  
  9. #include <exec/types.h>
  10. #include <dos/rdargs.h>
  11. #include <dos/dos.h>
  12. #include <dos/dosasl.h>
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16.  
  17. #define TEMPLATE "FILE/M,MINSIZE/K/N,MAXSIZE/K/N,LATIN1/S"
  18. #define ARGNUM 5
  19.  
  20. #define MAXSTRING 256
  21. #define MINSTRING 6
  22.  
  23. char VersionID[] = "$VER: Strings 1.2 (3.12.94)";
  24.  
  25. char ctable[] =
  26. {
  27.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  28.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  29.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  30.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  31.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  32.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  33.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  34.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  35.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  36.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  37.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  38.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  39.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  40.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  41.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  42.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  43. };
  44. LONG ArgArray[ARGNUM];
  45. ULONG minsize, maxsize, mask;
  46. STRPTR buf;
  47.  
  48. void String( BPTR );
  49.  
  50. int __main( void )
  51. {
  52.     STRPTR *files;
  53.     int rc = RETURN_OK;
  54.  
  55.     struct RDArgs *RDArgs = ReadArgs( TEMPLATE, ArgArray, NULL );
  56.     
  57.     minsize    = ArgArray[1] ? *(ULONG *)ArgArray[1] : MINSTRING;
  58.     maxsize    = ArgArray[2] ? *(ULONG *)ArgArray[2] : MAXSTRING;
  59.     mask    = ArgArray[3] ? 0x7f : 0xff;
  60.  
  61.     if ( maxsize < minsize )
  62.     {
  63.         PrintFault( ERROR_BAD_NUMBER, "Strings" );
  64.         rc = RETURN_FAIL;
  65.     }
  66.     else
  67.     {
  68.         if ( buf = (char *)AllocMem( maxsize + 2, 0 ) )
  69.         {
  70.             if (files = (char **)ArgArray[0])
  71.             {
  72.                 STRPTR filename;
  73.                 while ( filename = *(files++) )
  74.                 {
  75.                     BPTR in;
  76.                     if (in = Open( filename, MODE_OLDFILE ))
  77.                     {
  78.                         String( in );
  79.                         Close( in );
  80.                         if ( SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C )
  81.                             break;
  82.                     }
  83.                     else
  84.                     {
  85.                         PrintFault( IoErr(), "Strings" );
  86.                         rc = RETURN_WARN;
  87.                         break;
  88.                     }
  89.                 }
  90.             }
  91.             else
  92.             {
  93.                 String( Input() );
  94.             }
  95.             FreeMem( buf, maxsize + 2 );
  96.         }
  97.         else
  98.         {
  99.             PrintFault( IoErr(), "Strings" );
  100.             rc = RETURN_FAIL;
  101.         }
  102.     }
  103.     if ( SetSignal( 0L, 0L ) & SIGBREAKF_CTRL_C )
  104.     {
  105.         PrintFault( ERROR_BREAK, NULL );
  106.         rc = RETURN_ERROR;
  107.     }
  108.     FreeArgs( RDArgs );
  109.     return rc;
  110. }
  111.  
  112. void String( BPTR fh )
  113. {
  114.     LONG c;
  115.     STRPTR i = buf;
  116.     
  117.     while ( !(SetSignal(0L, 0L) & SIGBREAKF_CTRL_C) && ((c = FGetC( fh )) != -1) )
  118.     {
  119.         if (ctable[c & mask])
  120.         {
  121.             *(i++) = c;
  122.             if ( (i - buf) > maxsize )
  123.             {
  124.                 *(i++) = '\n'; *i = 0;
  125.                 PutStr( buf );
  126.                 i = buf;
  127.             }
  128.         }
  129.         else
  130.         {
  131.             if ( (i - buf) >= minsize )
  132.             {
  133.                 *(i++) = '\n'; *i = 0;
  134.                 PutStr( buf );
  135.             }
  136.             i = buf;
  137.         }
  138.     }
  139. }
  140.